package com.spider.silence.main;

import com.alibaba.fastjson.JSONObject;
import com.spider.silence.downloader.InformantCenterForSiChuanDownloader;
import com.spider.silence.pool.InformantCenterForSiChuanDownloaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.InformantCenterForSiChuanWorker;
import org.apache.log4j.PropertyConfigurator;
import us.codecraft.webmagic.Page;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by admin on 2017/7/3.
 * 四川互联网举报中心爬虫主方法
 */
public class InformantCenterForSiChuanMain {

    public static void main(String[] args) throws Exception {
        //初始化日志环境
        PropertyConfigurator.configure(PropertiesUtil.getLogPro());
        //初始化下载池
        InformantCenterForSiChuanDownloaderPool downloaderPool = InformantCenterForSiChuanDownloaderPool.getInstance();
        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取一个下载器
                InformantCenterForSiChuanDownloader downloader = null;
                try {
                    downloader = downloaderPool.fetchDownLoader(1000);
                    downloader.generateTask();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if(downloader != null) {
                        downloaderPool.releaseDownLoader(downloader);
                    }
                }
            }
        }).start();

        ExecutorService pool =  Executors.newFixedThreadPool(3000);
        //初始化中国新闻网四川频道的Worker并且添加到线程池中
        for(int i = 0; i<Integer.valueOf(PropertiesUtil.getVal("InformantCenterForSiChuan.workCount")); i++){
            pool.execute(new InformantCenterForSiChuanWorker());
        }

        /*InformantCenterForSiChuanDownloader downloader = new InformantCenterForSiChuanDownloader();
        JSONObject content = downloader.crawlItem("http://www.scjb.gov.cn/content.do?columnId=1034&contentId=10259&index=3");
        JSONObject data = content.getJSONObject("data");
        System.out.println(data.toJSONString());*/
    }

}
